<template>
    <div>
        <div class="container">
            <div class="handle-box">
                <el-input v-model="query.keyword" placeholder="商品名称 / 查询" class="handle-input mr10" clearable></el-input>
                <el-select v-model="query.isPublish" filterable remote placeholder="状态" class="mr10"
                @focus="func_getIsPublishOptionsLoad" :remote-method="(val)=>func_getIsPublishOptionsLoad" clearable>
                    <el-option v-for="item in isPublishOptions" :key="item.value" :label="item.label" :value="item.value" />
                </el-select>
                <el-button type="primary" icon="el-icon-search" @click="func_handleSearch" plain>搜索</el-button>
                <!-- <el-button type="danger" class="mr10" icon="el-icon-lx-add" @click="func_handleAdd" plain>添加</el-button> -->
				<!-- <el-button type="success" icon="el-icon-more" @click="func_jumpConfigPage" plain></el-button> -->
            </div>
            <el-table :data="productTableData" border class="table" header-cell-class-name="table-header">
                <el-table-column type="expand">
                <template #default="props">
                    <div m="4">
                        <span class="mr50">售价: {{ props.row.price }}</span>
                        <span class="mr50">会员价格: {{ props.row.uprice }}</span>
                        <span class="mr50">积分兑换值: {{ props.row.points }}</span>
                        <span class="mr50">运费: {{ props.row.shipmentPrice }}</span>
                        <span class="mr50">库存: {{ props.row.stocks }}</span>
                    </div>
                </template>
                </el-table-column>
				<el-table-column prop="id" label="ID" width="55" align="center" v-if="false"></el-table-column>
				<el-table-column label="序号" width="80" align="center"><template #default="scope">
						{{scope.$index + 1}}
				</template></el-table-column>
				<!-- <el-table-column label="展示图片" align="center" width="150px">
                    <template #default="scope">
                        <el-image style="padding: 5px 5px 5px 5px" 
						:src="scope.row.urlPic" fit="cover" 
						:preview-src-list="scope.row.contentPicList"
						@click.native="func_showBigPic(scope.$index, scope.row)"
						/>
                    </template>
				</el-table-column> -->
                <el-table-column prop="productBoardName" label="品牌名称" width="150px" align="left"></el-table-column>
				<el-table-column prop="productName" label="商品名称" align="left"></el-table-column>
                <el-table-column label="商品分类" >
                    <template #default="scope">
                        <el-tag class="mr10" type="success" v-for="item in scope.row.productClassifyName"> {{item}} </el-tag>
                    </template>
                </el-table-column>
				<el-table-column prop="price" label="售价" width="100px" align="right" v-if="false"></el-table-column>
				<el-table-column prop="uprice" label="会员价格" width="100px" align="right" v-if="false"></el-table-column>
                <el-table-column prop="points" label="积分兑换值" width="100px" align="right" v-if="false"></el-table-column>
				<el-table-column prop="shipmentPrice" label="运费" width="100px" align="right" v-if="false">
                    <template #default="scope">
                        {{scope.row.shipmentPrice === null ? 0 : scope.row.shipmentPrice }}
                    </template>
				</el-table-column>
				<!-- <el-table-column prop="productDescribe" label="描述" align="left"></el-table-column> -->
				<el-table-column prop="strShipmentProvince" label="发货省份" width="150px" align="center" />
                <el-table-column prop="strIsPublish" label="发布状态" width="150px" align="right" />
                <el-table-column label="操作" :width="optColumnWidth" align="center" >
                    <template #default="scope">
                        <el-button type="text" icon="el-icon-delete" class="red mr10" @click="func_handleDelete(scope.$index, scope.row)">删除</el-button>
                        <el-dropdown :hide-on-click="false">
                            <span class="el-dropdown-link green mr10"><el-icon class="el-icon-lx-info mr10" />更多</span>
                            <template #dropdown>
                                <el-dropdown-menu>
                                    <el-dropdown-item type="text" @click="func_handleEdit(scope.$index, scope.row)"> <el-icon class="el-icon-lx-add" />编辑</el-dropdown-item>
                                    <el-dropdown-item type="text" @click="func_handleSpec(scope.$index, scope.row)" class="green mr10"> <el-icon class="el-icon-lx-add" />配置规格</el-dropdown-item>
                                </el-dropdown-menu>
                            </template>
                        </el-dropdown>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination background layout="total, prev, pager, next" :current-page="query.pageIndex"
                    :page-size="query.pageSize" :total="pageTotal" @current-change="func_handlePageChange"></el-pagination>
            </div>
        </div>
        <!-- ---------------------------------------------------------------------------------------------------------------------- -->
        <!-- 图片弹出框 -->
        <el-dialog v-model="showPicVisible" append-to-body :width="bigPicWidth">
            <span>
                <el-card :body-style="{ padding: '7px' }"><el-image :src="bigPicUrl" class="image" @load="onBigPicLoad"/></el-card>
            </span>
        </el-dialog>
        <el-dialog v-model="vBoardsVisible" append-to-body>
            <VBoards @boardsDictData="func_loadvBoardsDictData" />
        </el-dialog>
    </div>
</template>
<script>
import { ref, reactive, toRefs } from 'vue';
import { useRouter, useRoute } from 'vue-router'
import { ElMessage, ElMessageBox } from 'element-plus';
import { findProductInfoListPage, batchEditProductPublish } from '../../../api/hairmanager/products';
import { findDictionaryInfoByDicCode, loadProductPublishStatusDict } from '../../../api/dict';

import { NO_DATA_SELECT_ERROR, SUCCESS_ADDDATA_DIALOG_MESSAGE, SUCCESS_DELDATA_DIALOG_MESSAGE, SUCCESS_EDITDATA_DIALOG_MESSAGE, formLabelWidth, } from "../../../api/CommonErrorMessage.js";

import VBoards from './components/boards.vue';
export default {
    name: "serviceProject",
    components: {VBoards, },
    setup() {
        const router = useRouter();
        const SUCCMESS_ADDDATA_DIALOG_MESSAGE = "添加数据成功";
        //主数据条件查询
        const query = reactive({
            keyword: '',
            isPublish: '',
            pageIndex: 1,
            pageSize: 10,
        });
        // 主数据总页数
        const pageTotal = ref(0);
        // 主数据表格
        let productTableData = ref([]);
        const optColumnWidth = 150;
        const showPicVisible = ref(false);
        const bigPicWidth = ref(0);
		const bigPicUrl = ref('');
        const isPublishOptions = ref([]);
        // =========================================================================================================
        const func_getIsPublishOptionsLoad = () => {
            loadProductPublishStatusDict().then((res) =>{
                res.data.forEach((item) => {
                    isPublishOptions.value.push({ value: item['value'], label: item['label'] });
                });                
            });
        }
        // 主业务处理 ========================================================================================= END
        /**
         * @name 获取表格数据
         */
        const func_getData = () => {
            findProductInfoListPage(query).then((res) => {
                productTableData.value = res.data.list;
                pageTotal.value = res.data.total;
            });
        };
        func_getData();
        /**
         * @name 主数据查询操作
         */
        const func_handleSearch = () => {
            query.pageIndex = 1;
            func_getData();
        };
        /**
         * @name 分页导航
         */
        const func_handlePageChange = (val) => {
            // query.pageIndex = val;
            query['pageIndex'] = val;
            func_getData();
        };
		const func_jumpConfigPage = () => {
			router.push("/configServiceParentProject");
		};
		/**
         * @name 主数据编辑对话框操作
         */
        const func_handleAdd = () => {
			router.push({path: '/productDetail', query:{id: -1 }});
        };
        /**
         * @name 读取商品详细数据
         */
        let idx = -1;
        const func_handleEdit = (index, row) => {
            idx = index;
            router.push({path: '/productDetail', query:{id: row.id}});
        };
        const func_handleSpec = (index, row) => {
            idx = index;
            router.push({path: '/productSpec', query:{id: row.id}});
        };
        /**
         * @name 删除商品数据操作
         */
        const func_handleDelete = (index, row) => {
            // 二次确认删除
            ElMessageBox.confirm("确定要删除吗？", "提示", {
            type: "warning",
            }).then(() => {
                func_delDialogEdit(row['id']);
            }).catch(() => {});
        };
        /**
         * @name 行内数据状态变更
         */
		const func_changeRowStatus = (index, row) => {
			if(index === -1){
				return;
			}else{
				if(index > -1){
					let obj = {'id':row['id'], 'disable':row['disable']};
					configServiceProjectDisable(obj).then((res) => {
						if(res.code != 200) {
							if(res.type == 0) {
								ElMessage.warning(res.data.msg);
							} else {
								ElMessage.error(res.data.msg);
							}
						} else {
							func_getData();
							ElMessage.success("修改成功");
						}
					});
				}
			}
		};
        /**
         * @name 删除商品配置
         */
        const func_delDialogEdit = (id) => {
            let vObj = {ids: [id], isPublish: 2013};
            batchEditProductPublish(vObj).then((res) => {
                if(res.code != 200) {
                    if(res.type == 0) {
                        ElMessage.warning(res.data.msg);
                    } else {
                        ElMessage.error(res.data.msg);
                    }
                } else {
                    ElMessage.success(SUCCESS_DELDATA_DIALOG_MESSAGE);
                    func_getData(); // 刷新数据
                }
            });
        };
        const func_showBigPic = (index, row) => {
            bigPicUrl.value = row.urlPic;
            showPicVisible.value = true;
        };
        // 主业务处理 ========================================================================================= END
        // 大图处理 ========================================================================================= START
        const onBigPicLoad = (e) => {
            const img = e.target;
            let width = 0;
            if (img.fileSize > 0 || (img.width > 1 && img.height > 1)) {
            width = img.width + 40;
            }
            bigPicWidth.value = width + 'px';
        };
        // 大图处理 ========================================================================================= END
        // 业务插件处理 ========================================================================================= START
        const vBoardsVisible = ref(false);
		const func_loadvBoardsDictData = (v) => {
			
		}
        // 业务插件处理 ========================================================================================= END
        // =========================================================================================================
        return {
            // 系统环境变量
            optColumnWidth, query, pageTotal, isPublishOptions,
            func_handleSearch, func_handlePageChange, func_getIsPublishOptionsLoad,
            // 主业务处理
            func_getData, productTableData, func_handleAdd, func_handleEdit, func_handleDelete,
            func_jumpConfigPage, func_handleSpec, func_delDialogEdit, func_changeRowStatus,
            // 大图业务处理
			bigPicUrl, bigPicWidth, showPicVisible, func_showBigPic, onBigPicLoad,
            // 业务插件处理
            vBoardsVisible, func_loadvBoardsDictData,
        };
    },
};
</script>
<style scoped>
.handle-box {
    margin-bottom: 20px;
}
.handle-select {
    width: 120px;
}
.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.green {
    color: #67c23a;
}
.mr10 {
    margin-right: 10px;
}
.mr50 {
    margin-right: 50px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>